package tdengine

import (
	"flag"
	"github.com/blagojts/viper"
	"github.com/spf13/pflag"
	"github.com/timescale/tsbs/pkg/data/serialize"
	"github.com/timescale/tsbs/pkg/data/source"
	"github.com/timescale/tsbs/pkg/targets"
	"github.com/timescale/tsbs/pkg/targets/constants"
)

func NewTarget() targets.ImplementedTarget {
	return &tdengineTarget{}
}

type tdengineTarget struct {
	options LoadingOptions
}

func (t *tdengineTarget) TargetName() string {
	return constants.FormatTDengine
}

func (t *tdengineTarget) Serializer() serialize.PointSerializer {
	return &Serializer{}
}

func (t *tdengineTarget) Benchmark(
	targetDB string, dataSourceConfig *source.DataSourceConfig, v *viper.Viper,
) (targets.Benchmark, error) {
	var loadingOptions LoadingOptions
	if err := v.Unmarshal(&loadingOptions); err != nil {
		return nil, err
	}
	return NewBenchmark(targetDB, &loadingOptions, dataSourceConfig)
}

func (t *tdengineTarget) TargetSpecificFlags(flagPrefix string, flagSet *pflag.FlagSet) {
	//flagSet.String(flagPrefix+"cfg", "config/cfg.toml", "configuration file which describes useCase and data format.")
	//flagSet.String(flagPrefix+"cases", "benchmark", "useCase for dataset to be imported. Multiple choices can be separated by comma, for example, -cases sensor_info,camera_detection.")
	flagSet.Int(flagPrefix+"Hnum", 100, "magnification factor of the sample tables. For example, if hnum is 100 and in the sample data there are 10 tables, then 10x100=1000 tables will be created in the database.")
	flagSet.Int(flagPrefix+"vnum", 1000, "copies of the sample records in each table. If set to 0，this program will never stop simulating and importing data even if the timestamp has passed current time.")
	flagSet.Int64(flagPrefix+"delay", DefaultDelay, "the delay time interval(millisecond) to continue generating data when vnum set 0.")
	flagSet.Int64(flagPrefix+"tick", 2000, "the tick time interval(millisecond) to print statistic info.")
	flagSet.Int(flagPrefix+"save", 0, "whether to save the statistical info into 'statistic' table. 0 is disabled and 1 is enabled.")
	flagSet.String(flagPrefix+"savetb", DefaultStatisticTable, "the table to save 'statistic' info when save set 1.")
	flagSet.Int(flagPrefix+"thread", 1, "number of threads to import data.")
	flagSet.Int(flagPrefix+"batch", 100, "rows of records in one import batch.")
	flagSet.Int(flagPrefix+"auto", 0, "whether to use the startTime and interval specified by users when simulating the data. 0 is disabled and 1 is enabled.")
	flagSet.String(flagPrefix+"start", "", "the starting timestamp of simulated data, in the format of yyyy-MM-dd HH:mm:ss.SSS. If not specified, the earliest timestamp in the sample data will be set as the startTime.")
	flagSet.Int64(flagPrefix+"interval", DefaultInterval, "time interval between two consecutive records, in the unit of millisecond. Only valid when auto is 1.")
	flagSet.String(flagPrefix+"host", "127.0.0.1", "tdengine server ip.")
	flagSet.Int(flagPrefix+"port", 6030, "tdengine server port.")
	flagSet.String(flagPrefix+"user", "root", "user name to login into the database.")
	flagSet.String(flagPrefix+"password", "taosdata", "the import tdengine user password")
	flagSet.Int(flagPrefix+"dropdb", 0, "whether to drop the existing database. 1 is yes and 0 otherwise.")
	flagSet.String(flagPrefix+"db", "", "name of the database to store data.")
	flagSet.String(flagPrefix+"dbparam", "", "database configurations when it is created.")
	flagSet.String(flagPrefix+"admin-db-name", "taosSql", "Database to connect to in order to create additional benchmark databases.\n"+
		"By default this is the same as the `user` (i.e., `postgres` if neither is set),\n"+
		"but sometimes a user does not have its own database.")

	flag.Parse()
}
